package cn.wind.xboot.framework.web.controller;


import cn.wind.xboot.core.res.ApiResult;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.Serializable;
import java.util.Arrays;

/**
 * <p>Title: Base</p>
 * <p>Description: TODO</p>
 *
 * @author xukk
 * @version 1.0
 * @date 2018/7/6
 */
public interface Base<E,ID extends Serializable> {
    /**
     * get service
     * @return
     */
    IService getService();

    /**
     * save
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    default ApiResult save(E entity){
        getService().save(entity);
        return ApiResult.Custom().success().add(entity);
    }
    /**
     * delete
     *
     * @param ids
     * @return
     */
    default ApiResult delete(@RequestParam Long[] ids) {
        getService().removeByIds(Arrays.asList(ids));
        return ApiResult.Custom().success("批量通过id删除数据成功");
    }

    /**
     *  edit
     * @param entity
     * @param ew
     * @return
     */
    @SuppressWarnings("unchecked")
    default ApiResult edit(E entity, Wrapper<E> ew) {
        getService().update(entity,ew);
        return ApiResult.Custom();
    }

    /**
     * get
     * @param iPage
     * @param ew
     * @return
     */
    @SuppressWarnings("unchecked")
    default ApiResult get(IPage<E> iPage,Wrapper<E> ew) {
        return ApiResult.Custom().success().add(getService().page(iPage,ew));
    }
}
